/****************************************************
 * @class DocumentSelectionWindow
 * @extends Ext.Window
 ***************************************************/
DocumentMultiSelectionWindow = Ext.extend(Ext.Window,{
	documentSelectionPanel: null,
	constructor: function(config){
		Ext.apply(this,config);
		
		this.documentSelectionPanel = new DocumentSelectionPanel({id: 'documentSelectionPanel'});
		
		DocumentMultiSelectionWindow.superclass.constructor.call(this, {
			layout: 'form',
			iconCls: 'cls_all_document_list',
			width: 640,
			height: 480,
			modal: true,
			closeAction: 'hide',
			plain: true,
			buttonAlign: 'center',
			resizable: false,
			layout: 'fit',
			items: [this.documentSelectionPanel],
			buttons: [{
			    text: "确定",
			    disabled: false,
			    scope: this,
			    handler: function(){
			    	this.hide();
			    	
			    	var _conllectDocument = Ext.fly("_collectDocument").dom.value;
			    	var dccLinkGrid = Ext.getCmp("dccLinkGrid");
			    	dccLinkGrid.load(_conllectDocument);
			    }
			},{
			    text: "关闭",
			    disabled: false,
			    scope: this,
			    handler: function(){
			    	this.hide();
			    }
			}]
		});
	},
	reset: function(){
		this.documentSelectionPanel.reset();
	}
});

/****************************************************
 * @class DocumentSelectionPanel
 * @extends Ext.FormPanel
 ***************************************************/
DocumentSelectionPanel = Ext.extend(Ext.FormPanel,{
	fuzzyDocSearch: null,
	objDocSelect: null,
	constructor: function(config){
		Ext.apply(this,config);
		
		this.objDocSelect = new DocSelect({id: 'docSelect'});
		this.fuzzyDocSearch = new FuzzyDocSearchPanel({id: 'fuzzyDocSearchPanel'});
		
		DocumentSelectionPanel.superclass.constructor.call(this, {
			region: 'center',
			border: false,
			width: 500,
			height: 400,
			layout: 'column',
			viewConfig: {forceFit: true},
			buttonAlign: 'center',
			items: [{
				columnWidth: 0.59,
				layout: 'fit',
				border: false,
				height: 410,
				items:[this.fuzzyDocSearch]
			},{
				columnWidth: 0.01,
				border: false,
				height: 410,
				layout: 'fit'
			},{
				columnWidth: 0.40,
				height: 410,
				layout: 'fit',
				items:[this.objDocSelect]
			}]
		});
	},
	reset: function(){
		this.objDocSelect.reload();
	}
});

/****************************************************
 * @class DocSelect
 * @extends Ext.grid.GridPanel
 ***************************************************/
DocSelect = Ext.extend(Ext.grid.GridPanel,{
	condition: null,
	cm: null,
	store: null,
	constructor: function(config){
		Ext.apply(this,config);
		
		this.cm = new Ext.grid.ColumnModel([
			new Ext.grid.RowNumberer(),
			{header:"文件编号", dataIndex:'documentNumber',width:100,sortable:true},
			{header:"文件名称", dataIndex:'documentName',width:150}
		]);
		
		var _list = document.getElementById("_collectDocument").value;
		this.condition = {documentList: _list};
		
		this.store = new Ext.data.Store({
		    baseParams: this.condition, 
		    proxy: new Ext.data.HttpProxy({
		    	method: 'POST',  
				url: '/pdm/dcc/getDocList.htm'
			}),
		    reader: new Ext.data.JsonReader({
				totalProperty: 'totalProperty',
				root: 'root',
				id: 'documentNumber'
			},[
				{name: 'documentNumber'},
		        {name: 'documentName'}
			])
		});
		
		this.store.load();
		
		DocSelect.superclass.constructor.call(this, {
			region: 'center',
			border: false,
			loadMask: true,
			width: 300,
			height: 500,
			cls: 'x-rb',
			viewConfig: {forceFit: true},
			ds: this.store,
			sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
			cm: this.cm,
			tbar: ['已选列表 (<font color=brown>提示：双击删除</font>)','->',{
				text: "清空",
				iconCls: 'cls_clean_all',
				handler:function(){
					document.getElementById("_collectDocument").value = "";
					var selectDoc = Ext.getCmp("docSelect");
					selectDoc.setDocs("");
					selectDoc.load();
				}
			}]
		});
		
		this.on('dblclick', function(grid, rowIndex, e){
			var row = this.getSelectionModel().getSelected();
			var collectDocument = document.getElementById("_collectDocument").value;
			
			var collectDocuments = collectDocument.split(",");
			var j = collectDocuments.length;
			for(var i=0;i < j;i++)
			{
				if(collectDocuments[i] == row.get("documentNumber"))
				{
					for(var t=i;t<j;t++)
					{
						collectDocuments[t]=collectDocuments[t+1];
					}
					j=j-1;
				}
			}
			if(j==0) collectDocument = "";
			else
			{
				for(var m=0;m<j;m++)
				{
					if(m==0) collectDocument=collectDocuments[m];
					else collectDocument = collectDocument+","+collectDocuments[m];
				}
			}
			document.getElementById("_collectDocument").value = collectDocument;
			this.condition.documentList = collectDocument;
			this.store.load();
		}); 
	},
	load: function() {
		this.store.load();
	},
	setDocs: function(c) {
		this.condition.documentList = c;
	},
	reload: function(){
		this.condition.documentList = "";
		this.store.load();
	}
});

/****************************************************
 * @class FuzzyDocSearchPanel
 * @extends Ext.grid.GridPanel
 ***************************************************/
FuzzyDocSearchPanel = Ext.extend(Ext.grid.GridPanel,{
	condition: null,
	cm: null,
	store: null,
	searchField: null,
	pagingToolbar: null,
	constructor: function(config){
		Ext.apply(this,config);
		
		this.cm = new Ext.grid.ColumnModel([
			new Ext.grid.RowNumberer(),
			{header:"文件编号", dataIndex:'documentNumber', width:80, sortable:true},
			{header:"文件名称", dataIndex:'documentName', width:125},
			{header:"制定人员", dataIndex:'requisitioner', width:95}
		]);
		
		this.condition = {};
		this.condition.limit = 50;
		
		this.store = new Ext.data.Store({
		    baseParams: this.condition,  
			proxy: new Ext.data.HttpProxy({
				method: 'POST',  
				url:'/pdm/dcc/getDccDocumentList.htm'
			}),
			reader: new Ext.data.JsonReader({
				totalProperty: 'totalProperty',
				root: 'root',
				id: 'documentNumber'
			},[
				{name: 'documentNumber'},
				{name: 'documentName'},
				{name: 'requisitioner'}
			])
		});
		
		this.store.load();
		
		this.searchField = new Ext.ux.form.SearchField({store: this.store, width:200});
		
		this.pagingToolbar = new Ext.PagingToolbar({
			pageSize: 50,
			store: this.store,
			displayInfo: true,
			displayMsg: "第{0}~{1}条,共{2}",
			emptyMsg: "没有记录",
			refreshText: "刷新",
			beforePageText: "第",
			afterPageText: "页,共{0}页",
			firstText: "第一页",
			prevText: "上一页",
			nextText: "下一页",
			lastText: "最后一页"
		})
		
		FuzzyDocSearchPanel.superclass.constructor.call(this, {
			region: 'center',
			border: true,
			loadMask: true,
			width: 300,
			height: 500,
			cls: 'x-rb',
			viewConfig: {forceFit: true},
			ds: this.store,
			sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
			cm: this.cm,
			tbar: ['文档查询:',this.searchField,'->',"<font color=brown>提示：双击选择</font>"],
			bbar: this.pagingToolbar
		});
		
		this.on('dblclick', function(rowIdx, r){
			var collectDocument = Ext.fly("_collectDocument").dom.value;
			
			var row = this.getSelectionModel().getSelected();
			var documentNumber = row.get("documentNumber")
			
			var selectDoc = Ext.getCmp("docSelect");
			var docStore = selectDoc.getStore();
			var docRecord = docStore.getById(documentNumber);
			if(docRecord!=null){
				Ext.Msg.alert("友情提醒:","文件编号["+documentNumber+"]的文件已经选择!");
			}else{
				if(collectDocument == "") collectDocument = documentNumber;
				else collectDocument = collectDocument + "," + documentNumber;
				
				document.getElementById("_collectDocument").value = collectDocument;
				selectDoc.setDocs(collectDocument);
				selectDoc.load();
			}
		});
	}
});